var fs = require('fs')

const electron = require('electron')
const dialog = electron.dialog
const BrowserWindow = electron.BrowserWindow


var localgamesfile = "localgames.json"
var localgames = initlocalgames();

function initlocalgames(){
    fs.exists(localgamesfile,function(exists){
        if(exists){
            localgames = JSON.parse(fs.readFileSync(localgamesfile,'utf8'))
        }else{
            var games = [
                {
                  id:1,
                  name:"月圆之夜",
                  develope:"Unity 2017.2.3",
                  wwise:"Wwise 2017.1.3.6377",
                  color:"green",
                  projects:{
                    main:{
                      id:1,
                      name:"Game Client",
                      local:"D:\\yyzy\\Client",
                      depot:"svn:\\xxxx.xxxx",
                      sourcectrl:"svn"
                    },
                    others:[
                      {
                        id:2,
                        name:"Character Editor",
                        local:"D:\\yyzy\\Character Editor",
                        depot:"svn:\\xxxx.xxxx",
                        sourcectrl:"git"
                      },
                      {
                        id:3,
                        name:"Scene Editor",
                        local:"D:\\yyzy\\Scene Editor",
                        depot:"svn:\\xxxx.xxxx",
                        sourcectrl:"perforce"
                      }
                    ]
                  }
                },
                {
                  id:2,
                  name:"光荣使命",
                  develope:"Unity 2017.2.3",
                  wwise:"Wwise 2017.1.3.6377",
                  color:"red",
                  projects:{
                    main:{id:1,name:"Client"},
                    others:[{id:2,name:"Character Editor"},{id:3,name:"Scene Editor"}]
                  },
                },
                {
                  id:3,
                  name:"龙珠手游",
                  develope:"Unity 2017.2.3",
                  wwise:"Wwise 2017.1.3.6377",
                  color:"blue",
                  projects:{
                    main:{id:1,name:"Dragonball"},
                    others:[{id:2,name:"Character Editor"},{id:3,name:"Scene Editor"}]
                  },
                }
            ]
            localgames = games;
        }
    })
    console.log("init project list ...")
}

function get_game(gameid){
  let game = localgames.find(function(item){
    return item.id == gameid
  })
  game.projects.main.gameid = gameid;
  Array.prototype.forEach.call(game.projects.others,function(item){
    item.gameid = gameid
  })
  return game;
}

function get_project(game,subid){
  if(subid == game.projects.main.id){
    return game.projects.main
  }else{
    return Array.prototype.find.call(game.projects.others,function(item){
      return item.id == subid
    })
  }
}

electron.ipcMain.on('get-all-games', (event, arg) => {
  event.returnValue = localgames
})


electron.ipcMain.on('get-game', (event, arg) => {
  event.returnValue = get_game(arg)
})

function open_game_project(event,gameid,subid){
  console.log("open game project:" + gameid + " of " + subid)
  let game = get_game(gameid)
  if (game){
    const window = BrowserWindow.fromWebContents(event.sender)
    const options = {
      type: 'info',
      title: game.name,
      message: "Open " + get_project(game,subid).name,
      buttons: ['是']
    }
    dialog.showMessageBox(window,options)
    //const files = dialog.showMessageBox(window, { properties: [ 'openFile' ]})
  }
}

function open_game_project_folder(event,gameid,subid){
  console.log("open wwise project:" + gameid + " of " + subid)
}

function open_wwise_project(event,gameid,subid){
  console.log("open wwise project:" + gameid + " of " + subid)
}

function open_assettracker(event,gameid,subid){
  console.log("open assettracker project:" + gameid + " of " + subid)
}

electron.ipcMain.on('project-commmand', (event, arg) => {
  switch(arg.operation){
    case "1"://Game Project
    open_game_project(event,arg.gameid,arg.projectid)
    break;
    case "2"://Game Project Folder
    open_game_project_folder(event,arg.gameid,arg.projectid)
    break;
    case "20"://Wwise Project
    open_wwise_project(event,arg.gameid,arg.projectid)
    break;
    case "30"://AssetTracker
    open_assettracker(event,arg.gameid,arg.projectid)
    break;
    default:
      console.log("something error:"+arg.operation)
  }
})

module.exports.save = function(){
  fs.writeFileSync(localgamesfile, JSON.stringify(localgames));
}